package cn.knightzz.controller;


import cn.hutool.core.util.ObjectUtil;
import cn.knightzz.entity.JsonResult;
import cn.knightzz.entity.SysExamine;
import cn.knightzz.entity.SysMember;
import cn.knightzz.service.SysMemberService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.http.cookie.SM;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 党员表 前端控制器
 * </p>
 *
 * @author knightzz
 * @since 2021-05-14
 */
@RestController
public class SysMemberController {
    @Resource
    SysMemberService memberService;

    @PostMapping("/member")
    public JsonResult<Object> addMember(@RequestBody SysMember member){
        Boolean status = memberService.addMember(member);
        if(status){
            return new JsonResult<>("200", "添加成功!");
        }else{
            return new JsonResult<>("201", "添加失败!");
        }
    }

    @PutMapping("/member")
    public JsonResult<Object> editMember(@RequestBody SysMember member){
        Boolean status = memberService.updateMember(member);
        if(status){
            return new JsonResult<>("200", "更新成功!");
        }else{
            return new JsonResult<>("201", "更新失败!");
        }
    }

    @DeleteMapping("/member/{id}")
    public JsonResult<Object> deleteMember(@PathVariable Long id){
        Boolean status = memberService.deleteMember(id);
        if(status){
            return new JsonResult<>("200", "删除成功!");
        }else{
            return new JsonResult<>("201", "删除失败!");
        }
    }

    @GetMapping("/member")
    public JsonResult<HashMap<String, Object>> selectMemberPage(@RequestParam("pageNum") Long pageNum,
                                                                 @RequestParam("pageSize") Long pageSize,
                                                                 @RequestParam("query") String query){
        IPage<SysMember> pages = memberService.selectMemberPage(pageNum, pageSize, query);
        JsonResult<HashMap<String, Object>> result;
        HashMap<String, Object> data = new HashMap<>();
        if(ObjectUtil.isNotNull(pages)){
            long total = pages.getTotal();
            List<SysMember> records = pages.getRecords();
            data.put("total", total);
            data.put("member", records);
            result = new JsonResult<>("200" , "查询成功");
        }else {
            data.put("total", 0);
            data.put("member", new ArrayList<>());
            result = new JsonResult<>("201" , "查询失败");
        }
        result.setData(data);
        return result;
    }

    @GetMapping("/member/name")
    public JsonResult<List<SysMember>> getMemberListLikeName(@RequestParam("query") String name){

        List<SysMember> memberList = memberService.getMemberListByName(name);
        JsonResult<List<SysMember>> result = new JsonResult<>("200" , "查询成功");
        result.setData(memberList);
        return result;
    }

}

